/*
 * Created by JFormDesigner on Sat May 16 18:17:48 CEST 2009
 */

package sd.settemmezzo.graphic;

import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import sd.settemmezzo.player.PlayerClient;


//import org.apache.log4j.Logger;
/**
 * @author FreeFab
 */
public class UI_Input extends JFrame {

	private static final long serialVersionUID = 1L;
	private String name;
	private String indirizzo;
	private String porta;
	
	public String getName() {
		return name;
	}

	public String getIndirizzo() {
		return indirizzo;
	}

	public String getPorta() {
		return porta;
	}

	public UI_Input() throws ParseException {
		initComponents();
		numPlayers.setEnabled(false);
		textField_nickname.requestFocus();
	}
	
	private void textField_IndirizzoActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	

	private void SalvaButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
	
		String nickname = textField_nickname.getText().replaceAll(" ", "");
		String indirizzoIP = textField_Indirizzo.getText();
		int porta, nplayers;
		//int porta = Integer.parseInt(textField_Porta.getText());

		if (nickname.equals("")){
			label_errore.setText("Inserisci un nickname");
		} else if (indirizzoIP.equals("")) {
			label_errore.setText("Inserisci un indirizzo");
		} else if (textField_Porta.getText().equals("")) {
			label_errore.setText("Inserisci una porta");
		} else if (actAsRS.isSelected() && numPlayers.getText().equals("")) {
			label_errore.setText("Inserisci il numero di giocatori");
		} else {
			try {
				porta = Integer.parseInt(textField_Porta.getText());
			} catch (NumberFormatException nfe) {
				label_errore.setText("Numero porta non valido");
				return;
			}
			
			try {
				nplayers = Integer.parseInt(numPlayers.getText());
				if (actAsRS.isSelected() && nplayers < 3) {
					label_errore.setText("I giocatori devono essere almeno 3");
					return;
				}
			} catch (NumberFormatException nfe) {
				label_errore.setText("Numero di giocatori non valido");
				return;
			}
			
			if (actAsRS.isSelected()) {
					JOptionPane.showMessageDialog(this, "Funzionalità server di registrazione non ancora implementata!\nUsare il server esterno.", "TODO", JOptionPane.WARNING_MESSAGE);
					return;
			}
			
			PlayerClient playerClient = new PlayerClient();
			playerClient.start(nickname, indirizzoIP, porta);

//			playerClient.start(nickname, "127.0.0.1", 1099);
			this.setVisible(false);
	
		}
	
	}

	private void actAsRSActionPerformed(ActionEvent e) {
		if (actAsRS.isSelected())
			numPlayers.setEnabled(true);
		else
			numPlayers.setEnabled(false);
	}

	private void actAsRSStateChanged(ChangeEvent e) {
		// TODO add your code here
	}
	
	public void initComponents() throws ParseException {
		
		String os = System.getProperty("os.name").toLowerCase();
        try {
            if (os.equals("linux")) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            } else if (os.startsWith("mac os")) {
                UIManager.getCrossPlatformLookAndFeelClassName();
            }
        } catch (Exception e) { }
		
        this.setResizable(false);
        
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		SalvaButton = new JButton();
		label_nickname = new JLabel();
		textField_nickname = new JTextField();
		label_errore = new JLabel();
		panel1 = new JPanel();
		label_indirizzo = new JLabel();
		textField_Indirizzo = new JTextField();
		label_Porta = new JLabel();
		textField_Porta = new JTextField();
		actAsRS = new JCheckBox();
		label_numPlayers = new JLabel();
		numPlayers = new JTextField();
		label1 = new JLabel();

		//======== this ========
		setTitle("Sette e Mezzo");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- SalvaButton ----
		SalvaButton.setText("Avvia il Gioco");
		SalvaButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SalvaButtonActionPerformed(e);
			}
		});
		contentPane.add(SalvaButton);
		SalvaButton.setBounds(125, 185, 135, 30);

		//---- label_nickname ----
		label_nickname.setText("Nickname:");
		label_nickname.setFont(label_nickname.getFont().deriveFont(label_nickname.getFont().getStyle() | Font.BOLD, label_nickname.getFont().getSize() + 4f));
		contentPane.add(label_nickname);
		label_nickname.setBounds(145, 105, 100, label_nickname.getPreferredSize().height);
		contentPane.add(textField_nickname);
		textField_nickname.setBounds(90, 140, 200, 25);

		//---- label_errore ----
		label_errore.setForeground(Color.red);
		label_errore.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(label_errore);
		label_errore.setBounds(75, 235, 235, 25);

		//======== panel1 ========
		{
			panel1.setBorder(new TitledBorder(" Registration Server "));
			panel1.setLayout(null);

			//---- label_indirizzo ----
			label_indirizzo.setText("Server IP:");
			label_indirizzo.setHorizontalAlignment(SwingConstants.RIGHT);
			panel1.add(label_indirizzo);
			label_indirizzo.setBounds(15, 25, 65, 25);

			//---- textField_Indirizzo ----
			textField_Indirizzo.setText("127.0.0.1");
			textField_Indirizzo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					textField_IndirizzoActionPerformed(e);
				}
			});
			panel1.add(textField_Indirizzo);
			textField_Indirizzo.setBounds(85, 25, 130, 25);

			//---- label_Porta ----
			label_Porta.setText(" Port:");
			label_Porta.setHorizontalAlignment(SwingConstants.RIGHT);
			panel1.add(label_Porta);
			label_Porta.setBounds(220, 25, 55, 25);

			//---- textField_Porta ----
			textField_Porta.setText("1099");
			panel1.add(textField_Porta);
			textField_Porta.setBounds(280, 25, 60, 25);

			//---- actAsRS ----
			actAsRS.setText("Act as registration server");
			actAsRS.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					actAsRSStateChanged(e);
				}
			});
			actAsRS.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					actAsRSActionPerformed(e);
				}
			});
			panel1.add(actAsRS);
			actAsRS.setBounds(20, 75, actAsRS.getPreferredSize().width, 25);

			//---- label_numPlayers ----
			label_numPlayers.setText("Players:");
			panel1.add(label_numPlayers);
			label_numPlayers.setBounds(255, 75, label_numPlayers.getPreferredSize().width, 25);

			//---- numPlayers ----
			numPlayers.setText("6");
			numPlayers.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
			numPlayers.setHorizontalAlignment(SwingConstants.CENTER);
			panel1.add(numPlayers);
			numPlayers.setBounds(310, 75, 30, 25);

			{ // compute preferred size
				Dimension preferredSize = new Dimension();
				for(int i = 0; i < panel1.getComponentCount(); i++) {
					Rectangle bounds = panel1.getComponent(i).getBounds();
					preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
					preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
				}
				Insets insets = panel1.getInsets();
				preferredSize.width += insets.right;
				preferredSize.height += insets.bottom;
				panel1.setMinimumSize(preferredSize);
				panel1.setPreferredSize(preferredSize);
			}
		}
		contentPane.add(panel1);
		panel1.setBounds(5, 280, 365, 120);

		//---- label1 ----
		label1.setBackground(Color.green);
		label1.setText("Sette e Mezzo");
		label1.setFont(new Font("Dialog", Font.ITALIC, 36));
		label1.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(label1);
		label1.setBounds(10, 5, 360, 110);

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(null);
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JButton SalvaButton;
	private JLabel label_nickname;
	private JTextField textField_nickname;
	private JLabel label_errore;
	private JPanel panel1;
	private JLabel label_indirizzo;
	private JTextField textField_Indirizzo;
	private JLabel label_Porta;
	private JTextField textField_Porta;
	private JCheckBox actAsRS;
	private JLabel label_numPlayers;
	private JTextField numPlayers;
	private JLabel label1;
	// JFormDesigner - End of variables declaration  //GEN-END:variables





}
